﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace cselstad_poc
{
    public partial class frmDB : Form
    {
        public string connectionString = "Data Source=sparky;Initial Catalog=cps420_team2_poc;Persist Security Info=True;User ID=cps420team2;Password=team2";
        public frmDB()
        {
            InitializeComponent();
        }

        private void btnLoadData_Click(object sender, EventArgs e)
        {
            AddressEntryDAO dao = new AddressEntryDAO();
            DataTable dt = dao.LoadRecords(connectionString);
            dataDB.DataSource = dt;
        }

        private void frmDB_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'cps420_team2_pocDataSet.AddressEntry' table. You can move, or remove it, as needed.
            this.addressEntryTableAdapter.Fill(this.cps420_team2_pocDataSet.AddressEntry);

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            AddressEntryDAO dao = new AddressEntryDAO();
            string name = txtName.Text;
            string email = txtEmail.Text;
            dao.ConnectDatabase(connectionString);
            if (name != "" && email != "")
            {
                AddressEntry entry = new AddressEntry(name, email);
                dao.CreateRecord(entry);
            }
            else {MessageBox.Show("Please give a name and email address.");}
            dao.CloseDatabase();
        }

        private void dataDB_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtName.Text = dataDB[0, e.RowIndex].Value.ToString();
            txtEmail.Text = dataDB[1, e.RowIndex].Value.ToString();
            txtNameInvis.Text = txtName.Text;
            txtEmailInvis.Text = txtEmail.Text;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            AddressEntryDAO dao = new AddressEntryDAO();
            string name = txtName.Text;
            string email = txtEmail.Text;
            dao.ConnectDatabase(connectionString);
            if (name != "" && email != "")
            {
                AddressEntry entry = new AddressEntry(name, email);
                dao.DeleteRecord(entry);
            }
            else { MessageBox.Show("Please give a name and email address."); }

            dao.CloseDatabase();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            AddressEntryDAO dao = new AddressEntryDAO();
            dao.ConnectDatabase(connectionString);
            if (txtEmail.Text != "" && txtName.Text != "" && txtEmailInvis.Text != ""
                && txtNameInvis.Text != "")
            {
                AddressEntry oldentry = new AddressEntry(txtNameInvis.Text, txtEmailInvis.Text);
                AddressEntry newentry = new AddressEntry(txtName.Text, txtEmail.Text);
                dao.UpdateRecord(oldentry, newentry);
            }
            else { MessageBox.Show("Please choose a record to edit."); }
            dao.LoadRecords(connectionString);
            dao.CloseDatabase();
        }
    }
}
